﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewLife.CommonEntity;
using XControl;
using BingZi.HY.Core.Entities.Center;
using NewLife.CommonEntity.Web;
using BingZi.HY.Core.Entities.System;
using BingZi.HY.Core.BLL;
using BingZi.HY.Core.Enums;

namespace BingZi.HY.UI.Admin.Manager
{

    public partial class Pages_app_CompanyTypeForm : PageBase
    {
        /// <summary>编号</summary>
        public Int32 EntityID { get { return NewLife.Web.WebHelper.RequestInt("CT_ID"); } }

        private app_CompanyType _Entity;
        /// <summary>系统管理员</summary>
        public app_CompanyType Entity
        {
            get { return _Entity ?? (_Entity = app_CompanyType.FindByKeyForEdit(EntityID)); }
            set { _Entity = value; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //绑定选择的枚举
                BingZi.HY.Core.WebCommon.WebHelper.BindList(frmState, typeof(EnumState));
                if (EntityID > 0)
                    BingZi.HY.Core.WebCommon.WebHelper.SelectedListByValue(frmState, Entity.State);
                else
                    BingZi.HY.Core.WebCommon.WebHelper.SelectedListByValue(frmState, (int)EnumState.Normal);


                DataBind();
              

                // 添加/编辑 按钮需要添加/编辑权限
                if (EntityID > 0)
                    UpdateButton.Visible = Acquire(PermissionFlags.Edit);
                else
                    UpdateButton.Visible = Acquire(PermissionFlags.New);
            }
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            // 添加/编辑 按钮需要添加/编辑权限
            if (EntityID > 0 && !Acquire(PermissionFlags.Edit))
            {
                NewLife.Web.WebHelper.Alert("没有编辑权限！");
                return;
            }
            if (EntityID <= 0 && !Acquire(PermissionFlags.New))
            {
                NewLife.Web.WebHelper.Alert("没有添加权限！");
                return;
            }

            if (!NewLife.Web.WebHelper.CheckEmptyAndFocus(frmName, "必须填写公司类型名称")) return;

            if (EntityID > 0)
            {
                int NameCount = app_CompanyType.FindCount(app_CompanyType._.Name, frmName.Text);
                if (NameCount > 1)
                {
                    NewLife.Web.WebHelper.AlertAndRefresh("公司类型名称已存在！");
                    return;
                }
                if (NameCount == 1)
                {
                    int keyid = app_CompanyType.Find(app_CompanyType._.Name, frmName.Text).CT_ID;
                    if (keyid != EntityID)
                    {
                        NewLife.Web.WebHelper.AlertAndRefresh("公司类型名称已存在！");
                        return;
                    }
                }
            }
            Entity.Name =    frmName.Text;
            Entity.ShowOrder = NewLife.Web.WebHelper.RequestInt( frmShowOrder.UniqueID);
            Entity.Description = frmDescription.Text;
            Entity.State = NewLife.Web.WebHelper.RequestInt(frmState.UniqueID);
 
            try
            {
                Entity.Save();
                //WebHelper.AlertAndRedirect("成功！", "Admin.aspx");
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('成功！');parent.Dialog.CloseAndRefresh(frameElement);", true);
            }
            catch (Exception ex)
            {
                NewLife.Web.WebHelper.Alert("失败！" + ex.Message);
            }
        }
    }
}

